package com.dc.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.project.entity.bo.TemplateDetailAttachmentBo;
import com.dc.project.entity.map.TemplateDetailAttachmentMapVo;
import com.dc.project.entity.po.TemplateDetailAttachment;
import com.dc.project.entity.query.TemplateDetailAttachmentQuery;
import com.dc.project.entity.vo.TemplateDetailAttachmentVo;
import com.dc.project.mapper.TemplateDetailAttachmentMapper;
import com.dc.project.service.TemplateDetailAttachmentService;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * TemplateDetailAttachmentServiceImpl
 *
 * @author chenxueli
 * @date 2025-03-19 13:23:08
 */
@Slf4j
@Service
public class TemplateDetailAttachmentServiceImpl extends AbstractBaseService<
        TemplateDetailAttachment,
        TemplateDetailAttachmentBo,
        TemplateDetailAttachmentVo,
        TemplateDetailAttachmentMapVo,
        TemplateDetailAttachmentQuery,
        TemplateDetailAttachmentMapper
        > implements TemplateDetailAttachmentService {

    @Resource
    private TemplateDetailAttachmentMapper templateDetailAttachmentMapper;

    /**
     * 获取查询条件
     *
     * @param query 查询条件
     * @return 查询条件对象
     */
    @Override
    public LambdaQueryWrapper<TemplateDetailAttachment> lambdaQueryWrapper(TemplateDetailAttachmentQuery query) {
        return super.lambdaQueryWrapper(query)
                .like(StringUtils.isNotBlank(query.getAttachmentName()), TemplateDetailAttachment::getAttachmentName, query.getAttachmentName())
                .eq(query.getTemplateDetailId() != null, TemplateDetailAttachment::getFkTemplateDetailId, query.getTemplateDetailId())
                .orderByDesc(TemplateDetailAttachment::getGmtCreate)
                .orderByDesc(TemplateDetailAttachment::getId);
    }

    /**
     * 根据模板详情id查询附件列表
     *
     * @param templateDetailId 模板详情id
     * @return 附件列表
     */
    @Override
    public List<TemplateDetailAttachment> listByTemplateDetailId(Long templateDetailId) {
        if (templateDetailId == null) {
            return List.of();
        }
        var wrapper = lambdaQueryWrapper()
                .eq(TemplateDetailAttachment::getFkTemplateDetailId, templateDetailId)
                .orderByDesc(TemplateDetailAttachment::getGmtCreate)
                .orderByDesc(TemplateDetailAttachment::getId);
        return templateDetailAttachmentMapper.selectList(wrapper);
    }

}
